package com.iap.client.mediator
{
	import com.iap.admin.event.TagEvent;
	import com.iap.admin.event.VectorEvent;
	import com.iap.admin.signal.VectorTagRequestSignal;
	import com.iap.app.AppGlobal;
	import com.iap.client.event.FilterEvent;
	import com.iap.client.event.TagRemainingRendererEvent;
	import com.iap.client.view.FilterView;
	import com.iap.client.view.tag.TagRemainingRenderer;
	import com.iap.common.mediator.BaseMediator;
	import com.iap.common.model.Tag;
	import com.iap.common.model.VectorModel;
	import com.iap.common.model.VectorTag;
	import com.iap.common.signal.TagRequestSignal;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;
	import mx.controls.Alert;
	
	public class FilterMediator extends BaseMediator
	{
		[Inject]
		public var view:FilterView;
		private var tags:Array;

		override public function onRegister():void
		{
			addContextListener(TagEvent.RESPONSE, tagResponseHandler);
			view.tagFilter.addEventListener(Event.CHANGE, tagFilterResponseHandler);
			view.tagFilter.addEventListener("removeItem", tagFilterResponseHandler);
			addContextListener(VectorEvent.RESPONSE, vectorResponseHandler);
			view.tagRemainingGrid.addEventListener(TagRemainingRendererEvent.CLICK, tagRemainingClickHandler);
		}
		
		private function tagResponseHandler(evt:TagEvent):void
		{
			AppGlobal.endWait();
			if(evt.response.task == TagRequestSignal.GET)
			{
				tags = evt.response.result as Array;
				view.tagData = new ArrayCollection(tags.slice());
				setInitialAvailableTag();
			}
		}
		
		private function tagFilterResponseHandler(evt:Event):void
		{
			tagFilterChanged();
		}
		
		private function tagFilterChanged():void
		{
			var tagData:Array = view.tagFilter.selectedItems.toArray();
			if(tagData.length == 0)
			{
				setInitialAvailableTag();
				return;
			}
			dispatch(new FilterEvent(FilterEvent.CHANGE, tagData));
		}
		
		private function vectorResponseHandler(evt:VectorEvent):void
		{
			setAvailableTagByVectors(evt.response.result as Array);
		}
		
		private function setAvailableTagByVectors(list:Array):void
		{
			var result:ArrayCollection = new ArrayCollection();
			var existingTags:Array = view.tagFilter.selectedItems.toArray();
			var existingTagNames:ArrayCollection = new ArrayCollection();
			for each(var tag:Tag in existingTags)
			{
				existingTagNames.addItem(tag.name);
			}
			
			for each(var vectorModel:VectorModel in list)
			{
				for each(var vtags:VectorTag in vectorModel.vectorTags)
				{
					var tagName:String = vtags.tag.name;
					if(!result.contains(tagName))
					{
						if(!existingTagNames.contains(tagName))
						{
							result.addItem(vtags.tag);
						}
					}
				}
			}
			
			view.tagRemainingGrid.dataProvider = result;
		}

		private function setInitialAvailableTag():void
		{
			var result:ArrayCollection = new ArrayCollection();
			for each(var item:Tag in tags)
			{
				result.addItem(item);
			}
			view.tagRemainingGrid.dataProvider = result;
		}
		
		private function tagRemainingClickHandler(evt:TagRemainingRendererEvent):void
		{
			view.tagFilter.selectedItems.addItem(evt.data);
			tagFilterChanged();
		}
	}
}